﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SLS.ExClassLib.Events;
using SLS.DataBase;
using SLS.ExClassLib.Message;
/*
 * 创建新纪录时，首先根据模板查询，然后将值传入新纪录
 */
namespace LanSunERP
{
    static class Extensions
    {
        /// <summary>
        /// Get substring of specified number of characters on the right.
        /// </summary>
        public static string Right(this string value, int length)
        {
            return value.Substring(value.Length - length);
        }
    }
    public partial class SQLFunction
    {
        public string logRoute = DateTime.Now + ":" + "LanSunERP.SQLFunction";
        public Dictionary<string, object> DicGlobalPara;
        public SLDBSET setGlobal;
        private SLQDBSET setQueryCond;
        public DataSet setReference = new DataSet();
        public DataSet dsResult;
        public DataTable dtRequest;
        public DataRow dwRequest;
        public MessageEx msgRequest;
        public DataSet setRequest;
        public string BaseTable;
        public DataRow dwResult;
        public string strSM;
        DataRow[] dwServiceLists = null;
        public DataTable TBForeignKeyList
        {
            get
            {
                return setGlobal.FOREIGNKEYLIST;
            }
        }
        private DataTable TBTableList
        {
            get
            {
                return setGlobal.TABLELIST;
            }
        }
        DataRow[] dwTableLists;
        DataRow[] dwForeignKeyLists;
        DataRow dwForeignKeyList;
        SqlDataAdapter tableadapter;
        string mySelectQuery;
        private SqlCommand cmdBuildingSQL;
        private SqlCommand cmdSQL;
        SqlConnection LanSunERPCon;
        SqlConnection LanSunERPQueryCondCon;
        public SqlConnection myConnection;

        public SQLFunction()
        {

        }

        public SQLFunction(Dictionary<string, object> DicGlobalPara)
        {
            try
            {
                dsResult = new DataSet();
                LanSunERPCon = (SqlConnection)DicGlobalPara["LanSunERPCon"];
                LanSunERPQueryCondCon = (SqlConnection)DicGlobalPara["LanSunERPQueryCondCon"];
                string TBName = "";
                BaseTable = ""; 
                this.DicGlobalPara = DicGlobalPara;
                this.msgRequest = (MessageEx)DicGlobalPara["msgRequest"];
                setRequest = msgRequest.setEvent;
                if (setRequest.Tables.Count > 0)
                {
                    setReference = new DataSet(msgRequest.JobID);
                    foreach (DataTable dtTemp in setRequest.Tables)
                    {
                        TBName = dtTemp.TableName;
                        if (TBName.Contains("QueryCond"))
                        {
                            BaseTable = TBName;
                        }
                        else
                        {
                            setReference = ImportDataTable(setReference, dtTemp);
                        }
                    }
                    if (BaseTable != "")
                    {
                        this.dtRequest = setRequest.Tables[BaseTable];
                    }
                    else
                    {
                        this.dtRequest = setRequest.Tables[0];
                    }
                    if (dtRequest.Rows.Count > 0)
                    {
                        dwRequest = dtRequest.Rows[0];
                        this.strSM = dwRequest["SM"].ToString();
                    }
                    else
                    {
                        dwRequest = dtRequest.NewRow();
                    }
                }
                else
                    this.dtRequest = null; 
                this.setGlobal = (SLDBSET)DicGlobalPara["dsParameters"];
                this.setQueryCond = new SLQDBSET(); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
     }
}
